package com.example.demo.poi;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.RichTextStringData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.example.demo.vo.TextMaterialExport;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
public class ExcelPoiController {

    @GetMapping("/poi")
    public void writer(HttpServletResponse response) throws IOException {


        TextMaterialExport textMaterialExport = new TextMaterialExport();

        // 设置单个单元格多种样式
        WriteCellData<String> richTest = new WriteCellData<>();
        richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING);
        textMaterialExport.setTitle(richTest);

        RichTextStringData richTextStringData = new RichTextStringData();
        richTest.setRichTextStringDataValue(richTextStringData);
        richTextStringData.setTextString("* 文本标题");

        // 前1个字红色
        WriteFont writeFont = new WriteFont();
        writeFont.setColor(IndexedColors.RED.getIndex());
        richTextStringData.applyFont(0, 1, writeFont);


        WriteFont writeFont2 = new WriteFont();
        writeFont2.setBold(Boolean.TRUE);
        writeFont2.setFontHeightInPoints((short) 16);
        richTextStringData.applyFont(writeFont2);

        //背景色
        WriteCellStyle writeCellStyleData = new WriteCellStyle();

        writeCellStyleData.setHorizontalAlignment(HorizontalAlignment.CENTER);

        richTest.setWriteCellStyle(writeCellStyleData);
        writeCellStyleData.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        writeCellStyleData.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

        //=================================================//

        // 设置单个单元格多种样式
        WriteCellData<String> richTest1 = new WriteCellData<>();
        richTest1.setType(CellDataTypeEnum.RICH_TEXT_STRING);
        textMaterialExport.setContent(richTest1);

        RichTextStringData richTextStringData1 = new RichTextStringData();
        richTest1.setRichTextStringDataValue(richTextStringData1);
        richTextStringData1.setTextString("* 文本内容");

        // 前1个字红色
        WriteFont writeFont1 = new WriteFont();
        writeFont1.setColor(IndexedColors.RED.getIndex());
        richTextStringData1.applyFont(0, 1, writeFont1);

        WriteFont writeFont3 = new WriteFont();
        writeFont3.setBold(Boolean.TRUE);
        writeFont3.setFontHeightInPoints((short) 16);
        richTextStringData1.applyFont(writeFont3);

        //背景色
        WriteCellStyle writeCellStyleData1 = new WriteCellStyle();

        writeCellStyleData1.setHorizontalAlignment(HorizontalAlignment.CENTER);

        richTest1.setWriteCellStyle(writeCellStyleData1);
        writeCellStyleData1.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        writeCellStyleData1.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

        List<TextMaterialExport> data = new ArrayList<>();
        data.add(textMaterialExport);


        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("文本素材导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TextMaterialExport.class).inMemory(true).sheet("模板").doWrite(data);

    }

}
